{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "#方法一：\n",
    "class Solution:\n",
    "    def isNumber(self, s: str) -> bool:\n",
    "        try:\n",
    "            a = float(s)\n",
    "        except ValueError:\n",
    "            return False\n",
    "        else:\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = Solution()\n",
    "print(a.isNumber(\"-5.23E34\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "#方法二：正则表达式\n",
    "import re\n",
    "#(:?exp)表示非捕获分组，非捕获分组不会将内容存储在内存中，速度更快。\n",
    "patt = '^[\\s]*(?#空格)[\\+\\-]?(?#符号)((\\d+\\.?)|(\\.\\d+)|(\\d+\\.\\d+))(?#底数)(e[\\+\\-]?\\d+)?(?#尾数)[\\s]*(?#空格)$'\n",
    "patt = '^[\\s]*(?#空格)[\\+\\-]?(?#符号)(?:(?:\\d+\\.?)|(?:\\.\\d+)|(?:\\d+\\.\\d+))(?#底数)(?:e[\\+\\-]?\\d+)?(?#尾数)[\\s]*(?#空格)$'\n",
    "class Solution:\n",
    "    def isNumber(self, s: str) -> bool:\n",
    "        matchObj = re.match(patt, s, re.I) #re.I表示不区分大小写\n",
    "        if matchObj:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "a = Solution()\n",
    "print(a.isNumber(\"-5.23E34\"))\n",
    "print(a.isNumber('3.'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1: yes  -5.23E+3 \n",
      "2: yes  +5e23\n",
      "3: yes 003.4\n",
      "4: yes 623457\n",
      "5: yes .3\n",
      "6: yes 3.\n",
      "7: yes 0\n",
      "8: no  0..\n",
      "9: no  e4\n",
      "10: no  4e\n",
      "11: no  4.e\n",
      "12: no  --23\n",
      "13: no  4e4.5\n",
      "14: no  35s3\n",
      "15: no  4ee3\n"
     ]
    }
   ],
   "source": [
    "# 用例测试\n",
    "patt = '^[\\s]*(?#空格)[\\+\\-]?(?#符号)((\\d+\\.?)|(\\.\\d+)|(\\d+\\.\\d+))(?#底数)(e[\\+\\-]?\\d+)?(?#尾数)[\\s]*$'\n",
    "patt = '^[\\s]*(?#空格)[\\+\\-]?(?#符号)(?:(?:\\d+\\.?)|(?:\\.\\d+)|(?:\\d+\\.\\d+))(?#底数)(?:e[\\+\\-]?\\d+)?(?#尾数)[\\s]*(?#空格)$'\n",
    "#patt = '^.*(E[\\+\\-]?[0-9]+)([\\s]*)$'\n",
    "# 空格 +- 数字 .数字 e +- 数字\n",
    "ls = [\n",
    "    ' -5.23E+3 ',\n",
    "    ' +5e23',\n",
    "    '003.4',\n",
    "    '623457',\n",
    "    '.3',\n",
    "    '3.',\n",
    "    '0',\n",
    "    '0..',\n",
    "    'e4',\n",
    "    '4e',\n",
    "    '4.e',\n",
    "    '--23',\n",
    "    '4e4.5',\n",
    "    '35s3',\n",
    "    '4ee3'\n",
    "    \n",
    "]\n",
    "i = 0\n",
    "for s in ls:\n",
    "    i += 1\n",
    "    matchObj = re.match(patt,s,re.I)\n",
    "    if matchObj:\n",
    "        print(str(i) + ': yes ' + s)\n",
    "    else:\n",
    "        print(str(i) + ': no  ' + s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
